package com.example.xcsubsystem.vo;

import org.springframework.http.HttpStatus;

/**
 * 2024/9/28
 *
 * @author fengyu
 */
public class ResBody<T> {
    /**
     * 默认成功响应码
     */
    private static final Integer DEAFAULT_SUCCESS_CODE = HttpStatus.OK.value();
    /**
     * 默认成功响应信息
     */
    private static final String DEAFAULT_SUCCESS_MSG = "请求/处理成功！";
    /**
     * 默认失败响应码
     */
    private static final Integer DEAFAULT_FAILURE_CODE = -1;
    /**
     * 默认失败响应信息
     */
    private static final String DEAFAULT_FAILURE_MSG = "请求/处理失败！";

    private Integer code;

    private String msg;

    private T data;

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
    /*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓成功↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/

    public ResBody success() {
        this.data = null;
        this.code = DEAFAULT_SUCCESS_CODE;
        return this;
    }


    /**
     * 处理成功响应，返回自定义响应码、信息和数据。
     *
     * @param msg  处理结果信息
     * @return 响应对象
     */
    public ResBody success(String msg) {
        this.msg = msg;
        this.code = DEAFAULT_SUCCESS_CODE;
        this.data = null;
        return this;
    }

    /**
     * 处理成功响应，返回自定义响应码、信息和数据。
     *
     * @param data 返回数据
     * @return 响应对象
     */
    public ResBody success(T data) {
        this.code = DEAFAULT_SUCCESS_CODE;
        this.data = data;
        return this;
    }

    /**
     * 处理成功响应，返回自定义响应码、信息和数据。
     *
     * @param msg  处理结果信息
     * @param data 返回数据
     * @return 响应对象
     */
    public ResBody success(String msg, T data) {
        this.msg = msg;
        this.code = DEAFAULT_SUCCESS_CODE;
        this.data = data;
        return this;
    }


    /*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓失败↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/

    /**
     * 处理失败响应，返回自定义响应码、信息和数据。
     *
     * @param msg 处理结果信息
     * @return 响应对象
     */
    public ResBody failure(String msg) {
        this.code = DEAFAULT_FAILURE_CODE;
        this.msg = msg;
        this.data = null;
        return this;
    }

    /**
     * 处理失败响应，返回自定义响应码、信息和数据。
     *
     * @param data 返回数据
     * @return 响应对象
     */
    public ResBody failure(T data) {
        this.code = DEAFAULT_FAILURE_CODE;
        this.data = data;
        return this;
    }

    /**
     * 处理失败响应，返回自定义响应码、信息和数据。
     *
     * @param msg  处理结果信息
     * @param data 返回数据
     * @return 响应对象
     */
    public ResBody failure(String msg, T data) {
        this.code = DEAFAULT_FAILURE_CODE;
        this.msg = msg;
        this.data = data;
        return this;
    }

    public ResBody failure(HttpStatus httpStatus, String msg) {
        this.code = httpStatus.value();
        this.msg = msg;
        return this;
    }
}
